package cc.iteachyou.template.taglib.directives;

import cc.iteachyou.template.configuration.TemplateEngineConfiguration;
import cc.iteachyou.template.dom.VirtualDOMAttr;
import cc.iteachyou.template.dom.VirtualDOMNode;
import cc.iteachyou.template.taglib.IDirectiveHandler;
import cc.iteachyou.template.taglib.result.DirectiveResult;
import cc.iteachyou.template.utils.TemplateHelper;

import java.util.Map;
import java.util.Optional;

/**
 * @author duanbowei123
 * @Date:2023/7/4
 * @Description:
 * @version：1.0
 **/
public class IncludeDirectiveHandler implements IDirectiveHandler {

    private TemplateEngineConfiguration configuration;
    @Override
    public Optional<DirectiveResult> handler(VirtualDOMNode.RendererVirtualDOMAttrContext ctx, Map<String, Object> data) {
        //TODO 此处暂时没用
        //直接通过模板引擎解析对应的html模板
        VirtualDOMAttr attr = ctx.getAttr();
        String value = attr.getValue();
        int i = value.indexOf(".", value.indexOf(".")+ 1);
        String substring = value.substring(value.indexOf("/")+1, i);
        TemplateHelper templateHelper = TemplateHelper.getInstance(configuration);
        String read = templateHelper.read(substring);
        DirectiveResult result = DirectiveResult.builder()
                .name(attr.getName())
                .value(read)
                .build();

        return Optional.of(result);
    }
}
